<?php
namespace app\core\controller;
use app\common\model\Verify;
use app\common\model\User as UserModel;

/**
 * 前台用户主页
 * @author laizhiyong <1029460965@qq.com>
 */
class User extends Common
{
    /**
     * 展示主页
     */
    public function main(){
        $this->assign('user',session('user'));
        return view();
    }

    /**
     * 更改密码
     */
    public function updatePassword(){
        if (request()->isAjax()){
            $opwd=input('post.opwd',false);
            $pwd=input('post.pwd',false);
            $rpwd=input('post.rpwd',false);
            $model=new UserModel();
            $info=$model->getUserByTel(session('user.tel'));
            if (createPwd($opwd)!==$info['pwd']){
                show(0,'旧密码不正确，请重新输入');
            }
            if ($pwd!==$rpwd){
                show(0,'新密码和确认密码不一致，请重新输入');
            }
            $data=[
                'pwd'=>createPwd($pwd)
            ];
            $where=[
                'id'=>session('user.id')
            ];
            if ($model->updateFieldsById($where,$data)){
                show(1,'更改成功');
            }
            show(0,'更改失败');
        }
        return view();
    }

    /**
     * 更改个人资料
     */
    public function editInfo(){
        if (request()->isAjax()){
            $data=input('post.');
            $model=new UserModel();
            $where=[
                'id'=>session('user.id')
            ];
            $model->updateFieldsById($where,$data);
            show(1,'保存成功');
        }
        $model=new UserModel();
        $data=$model->getUserByID(session('user.id'));
        $this->assign('data',$data);
        return view();
    }

    /**
     * 发送验证码
     */
    public function sendCode(){
        $verify=new Verify();
        $tel=input('post.tel');
        $res=$verify->sendTelCode($tel);
        if ($res['code']){
            show(1,$res['msg']);
        }
        show(0,$res['msg']);
    }

    /**
     * 验证信息更改手机
     */
    public function editTel(){
        if (request()->isPost()){
            $verify=new Verify();
            $tel=input('post.tel');
            $code=input('post.code');
            $res=$verify->Ver($tel,$code);
            if ($res['code']){
                $verify->delVer($tel);
                $model=new UserModel();
                $data=[
                    'tel'=>$tel
                ];
                $where=[
                    'id'=>session('user.id')
                ];
                $model->updateFieldsById($where,$data);
                show(1,'更改成功');
            }
            show(0,$res['msg']);
        }
        return view();
    }


    /**
     * 上传头像
     */
    public function upload(){
        if (request()->isPost()){
            $file = request()->file('fileImage');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $path = ROOT_PATH . 'public' . DS . 'uploads';
            $fileName = 'user'. DS .time().rand(1,999);
            $info = $file->move($path,$fileName);
            if($info){
                $userModel=new UserModel();
                $id=session('user.id');
                $filePath =  $info->getSaveName();
                $data = [
                    'imgurl'=>$filePath
                ];
                $where=[
                    'id'=>$id
                ];
                $field='imgurl';
                //获取原来的图片路径
                $old_url=$userModel->getOne($field,$where);
                $old_url=$old_url['imgurl'];
                $full_url=$path.DS.$old_url;
                $res=$userModel->updateFieldsById($where,$data);
                if($res){
                    //如果原图片存在，则进行删除
                    if (is_file($full_url)){
                        unlink($full_url);
                    }
                    $this->redirect('user/main');
                }else{
                    $this->error('上传失败,请重试!');
                }
            }else{
                // 上传失败返回错误信息
                $this->error($file->getError());
            }
        }
        return view();
    }
}

